package zadanie.Ch7homework.src;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Person {

	private String name;
	private String surname;
	private Date dateOfBirth;
	private Car car;

	Person(String name, String surname, Date dateOfBirth, Car car) {
		setName(name);
		setSurmane(surname);
		setDateOfBirth(dateOfBirth);
		setCar(car);
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return this.surname;
	}

	public void setSurmane(String surname) {
		this.surname = surname;
	}

	public Date getDateOfBirth() {
		return this.dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public Car getCar() {
		return this.car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public static Person[] parseArgsToPersons(String args[])
			throws java.text.ParseException {
		Person[] persons = new Person[args.length / 5];
		for (Integer i = 0; i < args.length / 5; i++) {
			persons[i] = new Person(args[i * 5 + 0], args[i * 5 + 1],
					parseDate(args[i * 5 + 2]), new Car(args[i * 5 + 3],
							parseDate(args[i * 5 + 4])));
		}
		return persons;
	}

	public static String[] parseFileToString(String filePath) {
		String inputData = null;
		String params[] = null;
		try {
			FileInputStream fstream = new FileInputStream(filePath);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			while ((inputData = br.readLine()) != null) {
				params = inputData.split(" ");
			}
			in.close();
		} catch (Exception e) {
			System.err.println(e);
		}
		return params;
	}

	private static Date parseDate(String dob) throws java.text.ParseException {
		DateFormat formatter;
		Date date = null;
		try {
			formatter = new SimpleDateFormat("dd/MM/yyyy");
			date = (Date) formatter.parse(dob);
		} catch (ParseException e) {
			System.out.println("Exception :" + e);
		}
		return date;
	}

	public static void greetPersons(Person[] persons) {
		for (Person person : persons) {
			person.introduceYourSelf(person);
		}
	}

	protected static Integer getCurrentYear() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar c1 = Calendar.getInstance();
		String currentYear = sdf.format(c1.getTime());
		return Integer.parseInt(currentYear);
	}

	private Integer getPersonAge(Person person) {
		Integer currectYear = getCurrentYear();
		String fullDateOfBirth = getDateOfBirth().toString();
		fullDateOfBirth = fullDateOfBirth
				.substring(fullDateOfBirth.length() - 4);
		Integer dateOfBirth = currectYear - Integer.parseInt(fullDateOfBirth);
		return dateOfBirth;
	}

	private void introduceYourSelf(Person person) {
		System.out.println("My name is " + getName() + " " + getSurname()
				+ ", I'm " + getPersonAge(person) + ". I drive "
				+ car.getBrand() + " which is " + car.getCarAge(car)
				+ " years old.");
	}

}
